package com.complex.webservice;



import com.complex.editejb.dao.IEditDAOBean;
import com.complex.getejb.dao.GetDAOBeanException;
import com.complex.getejb.dao.IGetDAOBean;
import com.complex.getejb.dao.TreeNode;
import com.complex.webservice.gen.*;
import org.springframework.stereotype.Component;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.List;


@Component("DaoService")
@WebService(targetNamespace = "http://gen.example.com/first", name = "DAOWebService")
public class DAOWebServer implements DAOWebService {

    private IGetDAOBean getDaoBean;
    private IEditDAOBean editDaoBean;

    @Override
    public void insertAttributeMethod(int inputNodeID, String inputAttributeName, String inputAttributeValue) {
        editDaoBean = DAOUtil.getIEditDAOBean();
        if(editDaoBean != null) {
            editDaoBean.insertAttribute(inputNodeID, inputAttributeName, inputAttributeValue);
        }
        editDaoBean = null;
    }

    @Override
    public void changeParentMethod(int inputNodeID, int inputNewParentID) {
        editDaoBean = DAOUtil.getIEditDAOBean();
        if(editDaoBean != null) {
            editDaoBean.changeParent(inputNodeID, inputNewParentID);
        }
        editDaoBean = null;
    }

    @Override
    public void insertNodeMethod(int inputParentID, String inputNodeName) {
        editDaoBean = DAOUtil.getIEditDAOBean();
        if(editDaoBean != null) {
            editDaoBean.insertNode(inputParentID, inputNodeName);
        }
        editDaoBean = null;
    }

    @Override
    public TreeNodeList getNodesMethod() throws GetDAOBeanException {

        List<TreeNode> beanNodes = null;
        TreeNodeList nodesList = new TreeNodeList();
        WebServiceTreeNode node;
        WebServiceMap nodeAttribs = null;
        MapItem attribItem = null;

        getDaoBean = DAOUtil.getIGetDAOBean();

        if (getDaoBean != null) {

            beanNodes = getDaoBean.getNodesList();

            for(int i = 0; i < beanNodes.size(); i++) {
                node = new WebServiceTreeNode();
                node.setNodeID(beanNodes.get(i).getNodeID());
                node.setNodeName(beanNodes.get(i).getNodeName());
                node.setParentID(beanNodes.get(i).getParentID());
                nodeAttribs = new WebServiceMap();
                for(int j = 0; j < beanNodes.get(i).getNodeAttributes().size(); j++) {
                    attribItem = new MapItem();
                    attribItem.setId(beanNodes.get(i).getNodeAttributes().get(j).getId());
                    attribItem.setKey(beanNodes.get(i).getNodeAttributes().get(j).getKey());
                    attribItem.setValue(beanNodes.get(i).getNodeAttributes().get(j).getValue());
                    nodeAttribs.getItem().add(attribItem);
                }
                node.setNodeAttributes(nodeAttribs);
                nodesList.getTNodeList().add(node);
            }
        }
        getDaoBean = null;

        return nodesList;
    }

    @Override
    public void editAttributeMethod(int inputAttributeID, String inputNewAttributeName, String inputNewAttributeValue) {
        editDaoBean = DAOUtil.getIEditDAOBean();
        if(editDaoBean != null) {
            editDaoBean.editAttribute(inputAttributeID, inputNewAttributeName, inputNewAttributeValue);
        }
        editDaoBean = null;
    }

    @Override
    public void deleteNodeMethod(List<String> inputAttributesIDList, List<String> inputNodesIDList) {
        editDaoBean = DAOUtil.getIEditDAOBean();
        if(editDaoBean != null) {
            editDaoBean.deleteNode(inputAttributesIDList, inputNodesIDList);
        }
        editDaoBean = null;
    }

    @Override
    public void deleteAttributeMethod(int inputAttributeID) {
        editDaoBean = DAOUtil.getIEditDAOBean();
        if(editDaoBean != null) {
            editDaoBean.deleteAttribute(inputAttributeID);
        }
        editDaoBean = null;
    }

    @Override
    public void editNodeMethod(int inputNodeID, String inputNewNodeName) {
        editDaoBean = DAOUtil.getIEditDAOBean();
        if(editDaoBean != null) {
            editDaoBean.editNode(inputNodeID, inputNewNodeName);
        }
        editDaoBean = null;
    }

    //-------------------------- inner class --------------------------------------------------
    private static class DAOUtil {

        private static IGetDAOBean getBean = null;
        private static IEditDAOBean editBean = null;

        public static IGetDAOBean getIGetDAOBean() {
            if (getBean == null) {
                try {
                    getBean = (IGetDAOBean) new InitialContext().lookup("GetDAOBeanName#com.complex.getejb.dao.IGetDAOBean");
                } catch (NamingException e) {
                    System.out.println("DAOUtil.getIGetDAOBean() Can't retrieve a getBean from context");
                    e.printStackTrace();
                }
            }
            return getBean;
        }

        public static IEditDAOBean getIEditDAOBean() {
            if (editBean == null) {
                try {
                    editBean = (IEditDAOBean) new InitialContext().lookup("EditDAOBeanName#com.complex.editejb.dao.IEditDAOBean");
                } catch (NamingException e) {
                    System.out.println("DAOUtil.getIEditDAOBean() Can't retrieve an editBean from context");
                    e.printStackTrace();
                }
            }
            return editBean;
        }
    }
    //-------------------------- inner class --------------------------------------------------
}
